﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;
using System.Drawing;
using System.IO;
using Model;
using DAL;
using System.Drawing.Imaging;

namespace UI.system.tools
{
    /// <summary>
    /// Upload 的摘要说明
    /// </summary>
    public class Upload : IHttpHandler, IRequiresSessionState
    {
        HttpContext content;
        public void ProcessRequest(HttpContext context)
        {
            string handle = context.Request["handle"];
            context.Response.ContentType = "text/plain";
            content = context;
            if (handle == "upload_product") //上传广告图片
            {
                string imgs = UploadImg();
                content.Response.Write(imgs);
            }
            else if (handle == "upload_product2") //上传广告图片
            {
                string imgs = UploadImg();
                content.Response.Write(imgs);
            }
            else if (handle == "upload_video") //上传文件(所有文件格式)
            {
                string files = UploadVideo(0);
                content.Response.Write(files);
            }
            else if (handle == "upload_video2") //上传文件(所有文件格式)
            {
                string files = UploadVideo(1);
                content.Response.Write(files);
            }

        }


        public string UploadImg()
        {
            int comid = JobRequest.GetRequestInt("comid");
            int userid = JobRequest.GetRequestInt("userid");
            string random = JobRequest.GetRequestString("random");
            string opath = "/upload/temp/" + DateTime.Now.ToString("yyyy-MM-dd") + "/" + random + "/";
            var rootpath = content.Server.MapPath(opath);
            if (!System.IO.Directory.Exists(rootpath))
            {
                Directory.CreateDirectory(rootpath);
            }
            List<string> filelist = new List<string>();
            for (int j = 0; j < content.Request.Files.Count; j++)
            {
                HttpPostedFile uploadFile = content.Request.Files[j];
                if (uploadFile.ContentLength > 0)
                {
                    string file = rootpath + uploadFile.FileName;
                    uploadFile.SaveAs(file);
                    filelist.Add(opath + uploadFile.FileName);
                }

            }
            return SerializeHelper.ToJsonByDynamic(filelist);
        }
        public string UploadVideo(int flag)
        {
            string random = JobRequest.GetRequestString("random");
            string opath = "";

            opath = "/upload/" + DateTime.Now.ToString("yyyy-MM-dd") + "/" + random + "/";
            var rootpath = content.Server.MapPath(opath);
            if (!System.IO.Directory.Exists(rootpath))
            {
                Directory.CreateDirectory(rootpath);
            }
            List<string> filelist = new List<string>();
            for (int j = 0; j < content.Request.Files.Count; j++)
            {
                HttpPostedFile uploadFile = content.Request.Files[j];
                if (uploadFile.ContentLength > 0)
                {
                    string filename = DateTime.Now.ToString("yyyyMMddHHmmssfff");
                    string ext = Path.GetExtension(uploadFile.FileName);
                    string file = rootpath + filename + ext;
                    string name = file.Substring(0, file.LastIndexOf('.'));
                    uploadFile.SaveAs(file);
                    filelist.Add(opath + filename + ext + "," + uploadFile.FileName);
                }

            }
            return SerializeHelper.ToJsonByDynamic(filelist);
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}